package com.company.hashtable;

/**
 * @author shenhan
 * @create 2020/2/2 21:38
 * @description
 */
public class HashTab {
    private EmpLinkedList[] empLinkedListArr;
    private int size;

    public HashTab(int size) {
        this.size = size;
        this.empLinkedListArr = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinkedListArr[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp) {
        int empLinkedListNo = hashFun(emp.id);
        empLinkedListArr[empLinkedListNo].add(emp);
    }

    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArr[i].list(i);
        }
    }

    public Emp find(int id) {
        int empLinkedListNo = hashFun(id);
        return empLinkedListArr[empLinkedListNo].findEmpById(id);
    }

    /**
     * 使用取模法实现散列函数
     *
     * @param id
     * @return
     */
    public int hashFun(int id) {
        return id % size;
    }
}
